<template>
	<view :class="['page',{'iphoneX':isIphoneX}]">  
		<view class="body">
			<template v-if="type==1">
				<template v-if="page=='add'||page=='edit'">
					<u--form :model="form" ref="uForm" class="form-wrap">
						<u-form-item label="产品类别" labelWidth="160" prop="pcategoryname" borderBottom="true" class="u-form-item" @click="pcategory.isShow=true">
							<u-input v-model="form.pcategoryname" placeholder="选择产品类别" disabled disabledColor="#ffffff" border="none" style="pointer-events: none;"/>
							<u-icon	slot="right" name="arrow-right"></u-icon>						
						</u-form-item>
						<u-form-item label="产品名称" labelWidth="160" prop="pname" borderBottom="true" class="u-form-item">
							<u-input v-model="form.pname" placeholder="输入产品名称" border="none" />
						</u-form-item> 
						<u-form-item label="单位" labelWidth="160" prop="unit_name" borderBottom="true" class="u-form-item" @click="unit.isShow=true">
							<u-input v-model="form.unit_name" disabled disabledColor="#ffffff" border="none" placeholder="选择单位" style="pointer-events: none;"/>
							<u-icon	slot="right" name="arrow-right"></u-icon>	
						</u-form-item>  
						<u-form-item label="子集单位" labelWidth="160" prop="min_unit_name" borderBottom="true" class="u-form-item" @click="minunits.isShow=true">
							<u-input :value="form.min_unit_name.join(',')" :placeholder="form.min_unit_name.length>0?'已选择':'请选择子集单位'" disabled disabledColor="#ffffff" border="none" style="pointer-events: none;"/>
							<u-icon	slot="right" name="arrow-right"></u-icon>		
						</u-form-item>  
						<u-form-item label="材料类别" labelWidth="160" prop="partstypename" borderBottom="true" class="u-form-item" @click="partstype.isShow=true"> 
							<u-input v-model="form.partstypename" placeholder="选择材料类别" disabled disabledColor="#ffffff" border="none" style="pointer-events: none;"/>
							<u-icon	slot="right" name="arrow-right"></u-icon>		
						</u-form-item> 
						<u-form-item label="材料配置" labelWidth="160" prop="partslistname" borderBottom="true" class="u-form-item" @click="selectparts">
							<u-input :placeholder="form.partslistname.length>0?'已选择':'请选择材料配置'" disabled disabledColor="#ffffff" border="none" style="pointer-events: none;"/>
							<u-icon	slot="right" name="arrow-right"></u-icon>		
						</u-form-item>  
						<u-form-item v-if="form.partslistname.length>0" label=" " prop="partslistname" borderBottom="true" class="u-form-item">
							<view class="select-pz-wrap">
								<view class="item" v-for="(item,index) in form.partslistname" :key="index" >
									<u--text :text="item.parts_type_name" :size="28"></u--text>
									<u--text :text="item.pname" :size="28"></u--text>
									<u--text :text="item.num+item.uname" :size="28"></u--text> 
									<u-icon class="del-icon" name="trash" color="#ff0000" size="30" @click="delpartslist(item)"></u-icon>
								</view>
							</view>					
						</u-form-item>
					</u--form>
				</template>
				
				<template v-if="page=='detail'">
					<u--form :model="form" ref="uForm" class="form-wrap m-b2">
						<u-form-item label="产品类别" labelWidth="160" prop="pcategoryname" borderBottom="true" class="u-form-item">
							<u-input v-model="form.pcategoryname" placeholder="选择产品类别" disabled disabledColor="#ffffff" border="none" />
							<!-- <u-icon	slot="right" name="arrow-right"></u-icon>						 -->
						</u-form-item>
						<u-form-item label="产品名称" labelWidth="160" prop="pname" borderBottom="true" class="u-form-item">
							<u-input v-model="form.pname" placeholder="输入产品名称" disabled border="none" disabledColor="#ffffff"/>
						</u-form-item> 
						<u-form-item label="单位" labelWidth="160" prop="unit_name" borderBottom="true" class="u-form-item">
							<u-input v-model="form.unit_name" disabled disabledColor="#ffffff" border="none" placeholder="选择单位" />
							<!-- <u-icon	slot="right" name="arrow-right"></u-icon>	 -->
						</u-form-item>  
						<u-form-item label="子集单位" labelWidth="160" prop="min_unit_name" borderBottom="true" class="u-form-item">
							<u-input :value="form.min_unit_name.join(',')" :placeholder="form.min_unit_name.length>0?'已选择':'未选择子集单位'" disabled disabledColor="#ffffff" border="none" />
							<!-- <u-icon	slot="right" name="arrow-right"></u-icon>		 -->
						</u-form-item>  
						<u-form-item label="材料类别" labelWidth="160" prop="partstypename" borderBottom="true" class="u-form-item"> 
							<u-input v-model="form.partstypename" placeholder="选择材料类别" disabled disabledColor="#ffffff" border="none" />
							<!-- <u-icon	slot="right" name="arrow-right"></u-icon>		 -->
						</u-form-item> 
						<u-form-item label="材料配置" labelWidth="160" prop="partslistname" borderBottom="true" class="u-form-item">
							<u-input placeholder="合成材料" disabled disabledColor="#ffffff" border="none" />
							<!-- <u-icon	slot="right" name="arrow-right"></u-icon>		 -->
						</u-form-item>  
						<u-form-item v-if="form.partslistname.length>0" label=" " labelWidth="160" prop="partslistname" borderBottom="true" class="u-form-item">
							<view class="select-pz-wrap">
								<view class="item" v-for="(item,index) in form.partslistname" :key="index" >
									<u--text :text="item.parts_type_name" :size="28"></u--text>
									<u--text :text="item.pname" :size="28"></u--text>
									<u--text :text="item.num+item.uname" :size="28"></u--text>  
								</view>
							</view>					
						</u-form-item>
					</u--form>	
					
					<!-- <u--form v-if="hp_count.length>0" class="form-wrap">
						<u-form-item :label="item.sname" labelWidth="160"  borderBottom="true" class="u-form-item" v-for="(item,index) in hp_count" :key="index">
							<u-input :value="item.p_count" disabled disabledColor="#ffffff" border="none" />
							<u--text :text="item.p_count+form.unit_name" size="30" align="right"></u--text>						
						</u-form-item>						 
					</u--form> -->
				</template>
			</template>
			
			<template v-if="type==2">
				<template v-if="page=='add'||page=='edit'">
					<u--form :model="form" ref="uForm" class="form-wrap"> 
						<u-form-item label="材料名称" labelWidth="160" prop="pname" borderBottom="true" class="u-form-item">
							<u-input v-model="form.pname" placeholder="输入材料名称" border="none" />
						</u-form-item> 
						<u-form-item v-if="page=='add'" label="入库数量" labelWidth="160" prop="number" borderBottom="true" class="u-form-item">
							<u-input v-model="form.number" disabled disabledColor="#ffffff" placeholder="输入入库数量" border="none" />
						</u-form-item> 
						<u-form-item v-if="page=='edit'" label="入库数量" labelWidth="160" prop="number" borderBottom="true" class="u-form-item">
							<u-input v-model="form.number" disabled disabledColor="#ffffff" placeholder="输入入库数量" border="none" />
						</u-form-item>
						<view class="row-2">
							<u-form-item label="最小单位" labelWidth="160" prop="unit_name" borderBottom="true" class="u-form-item" @click="unit.isShow=true">
								<u-input v-model="form.unit_name" readonly disabledColor="#ffffff" border="none" placeholder="选择单位" style="pointer-events: none;" />									
							</u-form-item>
							<u-button text="添加单位" type="success" size="small" class="btn" @click="addNewUnit"></u-button>
						</view>
						
						<view class="row-2">
							<u-form-item label="材料类别" labelWidth="160" prop="partstypename" borderBottom="true" class="u-form-item" @click="partstype.isShow=true"> 
								<u-input v-model="form.partstypename" placeholder="选择材料类别" disabled disabledColor="#ffffff" border="none" style="pointer-events: none;"/>										
							</u-form-item>
							<u-button text="添加类别" type="success" size="small" class="btn" @click="isAddtype=true"></u-button>	
						</view>
					</u--form>
				</template>
				
				<template v-if="page=='detail'">
					<u--form :model="form" ref="uForm" class="form-wrap">
						<u-form-item label="材料名称" labelWidth="160" prop="pname" borderBottom="true" class="u-form-item">
							<u-input v-model="form.pname" disabled disabledColor="#ffffff" placeholder="输入材料名称" border="none" />
						</u-form-item> 
						<u-form-item label="入库数量" labelWidth="160" prop="number" borderBottom="true" class="u-form-item">
							<u-input v-model="form.number" disabled disabledColor="#ffffff" placeholder="输入入库数量" border="none" />
						</u-form-item> 
						<u-form-item label="最小单位" labelWidth="160" prop="unit_name" borderBottom="true" class="u-form-item" >
							<u-input v-model="form.unit_name" disabled disabledColor="#ffffff" border="none" placeholder="选择单位" />									
						</u-form-item> 
						<u-form-item label="材料类别" labelWidth="160" prop="partstypename" borderBottom="true" class="u-form-item">
							<u-input v-model="form.partstypename" placeholder="选择材料类别" disabled disabledColor="#ffffff" border="none" />										
						</u-form-item> 
					</u--form>
				 		
				</template>
			</template>
			 
			
			<u-picker :show="pcategory.isShow" :columns="pcategory.data" keyName="name" :defaultIndex="pcategory.defaultIndex" itemHeight="90" :closeOnClickOverlay="true" @cancel="pcategory.isShow=false" @confirm="changepcategory" @close="pcategory.isShow=false"></u-picker>
			
			<u-picker :show="partstype.isShow" :columns="partstype.data" keyName="cname" :defaultIndex="partstype.defaultIndex" itemHeight="90" :closeOnClickOverlay="true" @cancel="partstype.isShow=false" @confirm="changepartstype" @close="partstype.isShow=false"></u-picker>
			<u-picker :show="unit.isShow" :columns="unit.data" keyName="uname" :defaultIndex="unit.defaultIndex" itemHeight="90" :closeOnClickOverlay="true" @cancel="unit.isShow=false" @confirm="changeunit" @close="unit.isShow=false"></u-picker>
			
			<view v-if="unit.isAdd" style="position: absolute;z-index: 10078;">
				<u-picker :show="unit.isAdd" :columns="unit.data" keyName="uname" :defaultIndex="unit.adddefaultIndex" itemHeight="90" :closeOnClickOverlay="true" @cancel="unit.isAdd=false" @confirm="changeaddunit" @close="unit.isAdd=false"></u-picker>
			</view>
			
			<u-popup :show="partslist.isShow" mode="right" class="popup-height">
				<view class="partslist-popup">
					<u--text class="hd" text="选择材料配置" size="30" :bold="true" align="center"></u--text>
					<view class="select-wrap">
						<view class="scroll-view">
							<u-button v-if="item.status==1"  class="item" :type="item.checked?'primary':'info'" :plain="true" size="small" :text="item.pname" v-for="(item,index) in partslist.data[0]" :key="index" @click="selectpartslist(index)"></u-button>
						</view> 
					</view>
					<view class="btn-wrap">
						<u-button text="取消" type="warning" size="small" class="btn" @click="partslist.isShow=false"></u-button>
						<u-button text="确定" type="primary" size="small" class="btn" @click="partslist.isShow=false"></u-button>
					</view>
				</view>
			</u-popup>	
			
			<u-popup :show="minunits.isShow" mode="right" :customStyle="popupCustomStyle" :safeAreaInsetBottom="true" :safeAreaInsetTop="true">
				<view class="partslist-popup">
					<u--text class="hd" text="选择子集单位" size="30" :bold="true" align="center"></u--text>
					<view class="select-wrap" :style="'height:'+ 'calc(100% - '+(100+systemInfo.statusBarHeight)+'px)'">
						<view class="scroll-view">
							<view class="item">
								<u-button :type="item.checked?'primary':'info'" :plain="true" size="small" :text="item.name" v-for="(item,index) in minunits.data" :key="index" @click="selectminunit(index)"></u-button>						  	     <u--input placeholder="请输入数量"  border="surround" ></u--input>
							</view>
							
						</view>
					</view>
					<view class="btn-wrap">
						<u-button text="取消" type="warning" size="small" class="btn" @click="minunits.isShow=false" ></u-button>
						<u-button text="确定" type="primary" size="small" class="btn" @click="minunits.isShow=false"></u-button>
					</view>
				</view>
			</u-popup>			 
					
			<u-modal :show="isAddUnit" title="添加单位" showCancelButton confirmText="确认添加" cancelText="暂不添加" @cancel="hideaddUnit" @confirm="addUnit">
				<view class="slot-content">
					<u--form :model="form" ref="uForm" class="form-wrap"> 
						<u-form-item label="选择级别" labelWidth="160" prop="add_unit_name" borderBottom="true" class="u-form-item"  @click="unit.isAdd=true">
							<u-input v-model="form.add_unit_name" disabled disabledColor="#ffffff" border="none" placeholder="选择级别" />
							<u-icon	slot="right" name="arrow-right"></u-icon>	
						</u-form-item>  
						<u-form-item label="单位名称" labelWidth="160" prop="uname" borderBottom="true" class="u-form-item">
							<u-input v-model="form.uname" placeholder="输入单位名称" border="none" />
						</u-form-item>
						<u-form-item v-if="form.add_unit_id!=0" label="比例" labelWidth="160" prop="pname" borderBottom="true" class="u-form-item">
							<u-input v-model="form.unit_count" placeholder="输入比例" border="none" />
							<u--text :text="form.add_unit_name" size="28"  align="center"></u--text>
						</u-form-item>
					</u--form> 
				</view>
			</u-modal>
			
			<u-modal :show="isAddtype" title="添加类别" showCancelButton confirmText="确认添加" cancelText="暂不添加" @cancel="isAddtype=false" @confirm="addType">
				<view class="slot-content">
					<u--form :model="form" ref="uForm" class="form-wrap"> 
						<u-form-item label="类别名称" labelWidth="160" prop="addtypeanme" borderBottom="true" class="u-form-item">
							<u-input v-model="form.addtypeanme" placeholder="输入类别名称" border="none" />
						</u-form-item> 						 
					</u--form> 
				</view>
			</u-modal>
			 
		</view>  
		<c-footer v-if="(page=='add'||page=='edit')"> 
			<view v-if="page=='add'" slot="content" class="add-btn" @click="add">提交</view>
			<view v-if="page=='edit'" slot="content" class="add-btn" @click="add">保存</view>
			
		</c-footer>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				id:'',
				isLogin:true, 
				isIphoneX:getApp().globalData.isIphoneX,
				isAddtype:false,
				isAddUnit:false,
				systemInfo:getApp().globalData.systemInfo,
				isShowSearch:false,
				type:'', 
				page:'',
				list:[], 
				form: {
					pcategoryid:'',
					pcategoryname:'',
					pname:'',
					partslist:[],
					partslistname:[],
					partstype:'',
					partstypename:'',
					unit_id:'',
					unit_name:'',
					add_unit_name:'',
					add_unit_id:'',
					number:'0',
					min_unit:[], 
					min_unit_name:[],
					status:1,
					addtypeanme:'', 
					uname:'',
					unit_count:''
				},
				pcategory:{
					isShow:false,
					data:[[]],
					defaultIndex:[]
				},
				partstype:{
					isShow:false,
					data:[[]],
					defaultIndex:[]
				},
				unit:{
					isAdd:false,
					isShow:false,
					data:[],
					defaultIndex:[],
					adddefaultIndex:[],
				},
				
				partslist:{
					isShow:false,
					data:[[]],
					defaultIndex:[]
				},
				minunits:{
					isShow:false,
					data:[],
					defaultIndex:[]
				},				 
				hp_count:[], 
				popupCustomStyle:{
					width:'100%'
				},
				selectParts:[],
				selectPartstype:[] 
			}
		},
		async onLoad(params) { 
			this.type=params.type;
			this.page=params.page; 
			let title=''; 
			if(this.type==1){
				this.productcategorylist();
				this.partstypes();
				this.unitlistsel(); 
				
				if(this.page=='add'){
					title='添加产品';					
				}
				if(this.page=='edit'){
					title='编辑产品';
					this.id=params.id; 
					this.producttypeinfo();
				}
				if(this.page=='detail'){
					title='产品详情';
					this.id=params.id;
					this.producttypeinfo();
				}
				uni.setNavigationBarTitle({
					title:title
				})				
			}
			if(this.type==2){
				this.productcategorylist();
				this.partstypes();
				this.unitlistsel(); 
				if(this.page=='add'){
					title='添加材料'
				}
				if(this.page=='edit'){
					title='编辑材料';
					this.id=params.id;
					this.partspartinfo();
				}
				if(this.page=='detail'){
					title='材料详情';
					this.id=params.id;
					this.partspartinfo();
				}
				uni.setNavigationBarTitle({
					title:title
				})
			}
		},
		onShow() {
			
		}, 
		methods: {  
			
			selectparts(){ 
				uni.navigateTo({
					url:`/package_kucun/list-select/list-select?page=parts&tname=${this.form.partstypename}&tid=${this.form.partstype}&current=${JSON.stringify(this.form.partslist)}`
				})
			},
			hideaddUnit(){
				this.isAddUnit=false;
				this.unit.data[0].splice(0,1);
			},
			addNewUnit(){ 
				this.isAddUnit=true;
				this.unit.data[0].unshift({
					id:0,
					uname:'最小单位'
				})
				this.form.add_unit_id=this.unit.data[0][0].id;
				this.form.add_unit_name=this.unit.data[0][0].uname;
			},
			async addUnit(){
				let {add_unit_id,uname,	unit_count}=this.form;
				if(uname==''){
					this.$showToast({
						title:'请输入单位名称',
						duration:1000,
					})
					return
				}
				if(add_unit_id!=0&&unit_count==''){
					this.$showToast({
						title:'请输入单位比例',
						duration:1000,
					})
					return
				}
				let res=await this.$api.user_api.partsaddunit({
					pid:add_unit_id,
					uname,
					unit_count,
					status:1
				}) 
				if(res.data.code==200){
					this.$showToast({
						title:res.data.msg,
						duration:1000,
					})
					this.isAddUnit=false;
					this.form.uname='';
					this.form.unit_count='';
					this.unit.data[0].splice(0,1);
					this.unitlistsel(); 
				}
			},
			
			async addType(){
				if(this.form.addtypeanme==''){
					this.$showToast({
						title:'请输入类别名称',
						duration:1000,
					})
					return
				}
				let res=await this.$api.user_api.partsaddtype({
					cname:this.form.addtypeanme,
					status:1
				}) 
				if(res.data.code==200){
					this.$showToast({
						title:res.data.msg,
						duration:1000,
					})
					this.isAddtype=false;
					this.form.addtypeanme='';
					this.partstypes();
				}
			},
			
			selectminunit(idx){
				let id=this.minunits.data[idx].id,
					name=this.minunits.data[idx].name;
				if(this.form.min_unit.indexOf(id)==-1){ 
					this.form.min_unit.push(id);
					this.form.min_unit_name.push(name);
					this.minunits.data[idx].checked=true;
				}else{ 
					let index=this.form.min_unit.indexOf(id);
					this.form.min_unit.splice(index,1);
					this.form.min_unit_name.splice(index,1);
					this.minunits.data[idx].checked=false;
				}
			},
			
			async producttypeinfo(){
				let res=await this.$api.user_api.producttypelistone({
					id:this.id
				})   
				console.log(res.data.data)
				if(res.data.code==200){
					this.form.pname=res.data.data.pname;
					this.form.pcategoryid=res.data.data.pcategoryid;
					this.form.pcategoryname=res.data.data.pcategory;
					this.hp_count=res.data.data.hp_count;
					 
					res.data.data.partslist.forEach(i=>{
						let item={};
						item.parts_type_name=i.type_name;
						item.pname=i.parts_name;
						item.num=i.num;
						item.uname=i.uname;  
						this.form.partslistname.push(item);
					})
					 
					res.data.data.unit_id=res.data.data.unit_id.split(','); 
					res.data.data.unit_id.forEach((item,index)=>{
						this.unit.data[0].forEach(async (subitem,subindex)=>{
							if(item==subitem.id){
								this.form.unit_id=subitem.id;
								this.form.unit_name=subitem.uname;
								this.unit.defaultIndex=[subindex];
								
							}							
						}) 
					}) 
					await this.subunitlistsel(this.form.unit_id); 
					
					res.data.data.unit_id.forEach((item,index)=>{
						this.minunits.data.forEach(subitem=>{
							if(item==subitem.id){
								this.form.min_unit.push(subitem.id);
								this.form.min_unit_name.push(subitem.name); 
								subitem.checked=true;
							}							
						}) 
					}) 
					 
					if(res.data.data.partstype){
						this.form.partstype=res.data.data.partstype;
						await this.getpartslist(this.form.partstype);
						this.partstype.data[0].forEach((item,index)=>{
							if(item.id==res.data.data.partstype){
								this.form.partstypename=item.cname;
								this.partstype.defaultIndex=[index]; 
							}
						}) 
					}
					
					this.form.partslist=res.data.data.partslist.split(',');	 
					this.form.partslist.forEach(item=>{
						this.partslist.data[0].forEach(subitem=>{
							if(item==subitem.id){
								this.form.partslistname.push(subitem.pname);
								subitem.checked=true;
							}
						})						
					}) 					 
				}
			},
			
			async partspartinfo(){
				let res=await this.$api.user_api.partspartinfo({
					id:this.id
				})     
				if(res.data.code==200){
					this.form.pname=res.data.pdata.pname;
					this.form.number=res.data.pdata.number;  
					this.form.unit_id=res.data.pdata.min_unit;  
					this.unit.data[0].forEach((subitem,subindex)=>{
						if(this.form.unit_id==subitem.id){
							this.form.unit_id=subitem.id;
							this.form.unit_name=subitem.uname;
							this.unit.defaultIndex=[subindex];
							
						}							
					})  					 
					this.form.partstype=res.data.pdata.typeid; 
					this.partstype.data[0].forEach((item,index)=>{
						if(item.id==this.form.partstype){
							this.form.partstypename=item.cname;
							this.partstype.defaultIndex=[index]; 
						}
					})   					 
				}
			},
			
			async add(){
				let {pname,pcategoryid,partslist,partstype,unit_id,min_unit,number}=this.form;
				let p_partslist=[]; 
				partslist.forEach(i=>{
					p_partslist.push(i.parts_id);
				})
				let res={};
				if(this.type==1){
					if(pname==''){
						this.$showToast({
							title:'请输入产品名称',
							duration:1000,
						})
						return
					}
					if(partslist.length==0){
						this.$showToast({
							title:'请选择材料配置',
							duration:1000,
						})
						return
					} 
					if(this.page=='add'){
						min_unit.push(unit_id); 
						res=await this.$api.user_api.producttypeadd({
							pname,
							pcategoryid,
							partslist:p_partslist.join(','),
							partslistls:JSON.stringify(partslist),
							partstype:this.selectPartstype.join(','),
							unit_id:min_unit.join(',')
						}) 
					} 
					if(this.page=='edit'){ 
						min_unit.push(unit_id);
						res=await this.$api.user_api.producttypeedit({
							id:this.id,
							pname,
							pcategoryid,
							partslist:p_partslist.join(','),
							partslistls:JSON.stringify(partslist),
							partstype:this.selectPartstype.join(','),
							unit_id:min_unit.join(',')
						}) 
					}  
					if(res.data.code==200){
						this.$showToast({
							icon:'success',
							title:res.data.msg,
							duration:1000,
						})
						this.$store.commit('KUCUN_LIST_UPDATE',1);
						setTimeout(()=>{
							uni.navigateBack({
								delta:1
							})
						},300)						
					}
					
				}
				
				if(this.type==2){
					if(pname==''){
						this.$showToast({
							title:'请输入材料名称',
							duration:1000,
						})
						return
					}
					if(number==''){
						this.$showToast({
							title:'请输入数量',
							duration:1000,
						})
						return
					}
					if(this.page=='add'){ 
						res=await this.$api.user_api.partsaddpart({
							pname,
							number, 
							typeid:partstype,
							min_unit:unit_id,
							status:1
						})  
					}
					if(this.page=='edit'){ 
						res=await this.$api.user_api.partseditpart({
							id:this.id,
							pname,
							number,
							typeid:partstype,
							min_unit:unit_id,
							status:1
						}) 
					}   
					if(res.data.code==200){
						this.$showToast({
							icon:'success',
							title:res.data.msg,
							duration:1000,
						})
						this.$store.commit('KUCUN_LIST_UPDATE',1);
						setTimeout(()=>{
							uni.navigateBack({
								delta:1
							})
						},300)						
					}
				}
			},
			
			delpartslist(name){ 
				this.partslist.data[0].forEach((item,index)=>{
					if(name==item.pname){
						item.checked=false; 
					}
				})
				let idx=this.form.partslistname.indexOf(name);
				this.form.partslistname.splice(idx,1);
				this.form.partslist.splice(idx,1);				
			},
			
			selectpartslist(idx){ 
				let id=this.partslist.data[0][idx].id,
					pname=this.partslist.data[0][idx].pname;
				if(this.form.partslist.indexOf(id)==-1){ 
					this.form.partslist.push(id);
					this.form.partslistname.push(pname);
					this.partslist.data[0][idx].checked=true;
				}else{ 
					let index=this.form.partslist.indexOf(id);
					this.form.partslist.splice(index,1);
					this.form.partslistname.splice(index,1);
					this.partslist.data[0][idx].checked=false;
				}
				
			},
			
			changepartslist(e){ 
				this.partslist.defaultIndex=e.indexs;
				this.form.partslist=e.value[0].id;
				this.form.partslistname=e.value[0].pname;
				this.partslist.isShow=false;				
			},
			
			changepartstype(e){  
				this.partstype.defaultIndex=e.indexs;
				this.form.partstype=e.value[0].id;
				this.form.partstypename=e.value[0].cname;
				this.partstype.isShow=false;
				// this.form.partslist=[];
				// this.form.partslistname=[];
				this.getpartslist(this.form.partstype);
			},
			
			changeunit(e){ 
				this.unit.defaultIndex=e.indexs;
				this.form.unit_id=e.value[0].id;
				this.form.unit_name=e.value[0].uname;
				this.form.min_unit_name=[];
				this.form.min_unit=[];
				this.unit.isShow=false;
				this.subunitlistsel(this.form.unit_id);
			},
			
			changeaddunit(e){
				this.unit.adddefaultIndex=e.indexs;
				this.form.add_unit_id=e.value[0].id;
				this.form.add_unit_name=e.value[0].uname;
				this.unit.isAdd=false;
				this.form.min_unit=[];
				this.form.min_unit_name=[];
			},
			 
			changepcategory(e){ 
				this.pcategory.defaultIndex=e.indexs;
				this.form.pcategoryid=e.value[0].id;
				this.form.pcategoryname=e.value[0].name;
				this.pcategory.isShow=false;
			},
			
			async productcategorylist(){
				let res=await this.$api.user_api.productcategorylist();   
				if(res.data.code==200){  
					this.$set(this.pcategory.data,0,res.data.data); 
					this.pcategory.defaultIndex=[0];
					this.form.pcategoryid=res.data.data[0].id;
					this.form.pcategoryname=res.data.data[0].name; 
				}
			}, 
			
			async partstypes(){
				let res=await this.$api.user_api.partstypes({
					status:1
				});   
				if(res.data.code==200){  
					this.$set(this.partstype.data,0,res.data.data); 
					this.partstype.defaultIndex=[0];
					this.form.partstype=res.data.data[0].id;
					this.form.partstypename=res.data.data[0].cname;
					this.getpartslist(this.form.partstype); 
				}
			},
			
			async unitlistsel(){
				let res=await this.$api.user_api.unitlistsel({
					pid:0
				});      
				if(res.data.code==200){ 
					
					this.$set(this.unit.data,0,res.data.data);
					this.unit.defaultIndex=[0];
					this.unit.adddefaultIndex=[0];
					this.form.unit_id=res.data.data[0].id;
					this.form.unit_name=res.data.data[0].uname;	
					this.form.add_unit_name=res.data.data[0].uname;	
					this.form.add_unit_id=res.data.data[0].id;	
					this.subunitlistsel(this.form.unit_id)
				}
			}, 
			
			async subunitlistsel(pid){
				let res=await this.$api.user_api.unitlistsel({
					pid:pid
				});     
				if(res.data.code==200){  					  
					res.data.data.forEach(item=>{
						item.name=item.unit_count+item.uname;
						item.checked=false;
					})
					this.minunits.data=res.data.data;
					 
				}
			},
			
			async getpartslist(tid){
				let res=await this.$api.user_api.partslist({
					typeid:tid,
					is_page:''
				}); 				
				if(res.data.code==200){  
					res.data.data.list.forEach(item=>{
						item.checked=false;
					})
					this.$set(this.partslist.data,0,res.data.data.list);   
				} 
			},
			
			  
		},
		computed:{
			list_update(){
				return this.$store.state.caigou_msg_list;
			},
			parts_select(){
				return this.$store.state.parts_select;
			}
			
		},
		watch:{
			parts_select(newer,older){  
				if(newer!=older){
					let index=this.selectParts.findIndex(i=>i.parts_type === newer.parts_type); 
					if(index>=0){
						this.selectParts.splice(index,1,newer)
					}else{
						this.selectParts.push(newer)
					}
					
					this.form.partslist=[];
					this.form.partslistname=[];
					this.selectPartstype=[];
					this.selectParts.forEach(i=>{	
						this.selectPartstype.push(i.parts_type);
						if(i.data.length>0){
							i.data.forEach(j=>{
								let item={};
								item.parts_id=j.id;
								item.parts_type=j.parts_type; 
								item.num=j.num;
								this.form.partslist.push(item);
								this.form.partslistname.push(j);
							}) 
						}	 
					}) 
				} 
			} 
		}
	}
</script>

<style lang="less" >
	.form-wrap{
		padding:0 20rpx; 
		.row-2{
			display: flex;
			justify-content: space-between;
			align-items: center;
		}
	}
	.slot-content{
		.hd{
			padding: 30rpx 0 10rpx;
			font-size: 30rpx;
		}
	}
	.popup-form-wrap{
		padding:20rpx;
		border-radius: 15rpx;		 
	}
	.select-pz-wrap{  
		flex-grow: 2;
		.item{
			flex-grow: 2;
			display: flex; 
			align-items: center;
			margin:0 20rpx 20rpx 0;
			font-size: 30rpx;
			.del-icon{ 
			}
		}
	}
	.add-btn{
		height: 36px;
		line-height: 36px;
		width: 96%;
		margin: 5px auto;
		background: #1cab1b;
		border-radius: 8rpx;
		color: #fff;
		font-size: 30rpx;
		text-align: center;
	}  
	.partslist-popup{  
		position: relative; 
		height: 100%;  
		padding: 0 40rpx; 
		.hd{  
			height: 40px; 
		}
		.select-wrap{  
			height:80%;
			flex-grow: 2; 
			overflow: hidden; 
			background: #f5f5f5;
			.scroll-view{
				height: 100%;
				overflow-y: auto; 
			}
			.item{ 
				margin-bottom: 20rpx;
			}
		}
		.btn-wrap{  
			height: 40px;
			margin: 20rpx 0;
			.btn{
				margin: 0 30rpx;
			}
		}
	}
</style>
